package yx.action;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;

import yx.service.TrainingServiceInt;
import yx.vo.JsonVO;
import yx.vo.TrainingVO;

import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("basePackage")
@Namespace("/")
@Action(value = "trainingAction")
public class TrainingAction extends BaseAction implements ModelDriven<TrainingVO> {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(TrainingAction.class);

	private TrainingVO training = new TrainingVO();

	@Override
	public TrainingVO getModel() {
		return training;
	}

	private TrainingServiceInt trainingService;

	public TrainingServiceInt getTrainingService() {
		return trainingService;
	}

	@Autowired
	public void setTrainingService(TrainingServiceInt trainingService) {
		this.trainingService = trainingService;
	}

	public void queryTraining() {
		/*
		 * TODO：需要调查为什么加判定
		 */
		if (training.getStudentId() != null) {
			super.writeJson(trainingService.datagrid(training));
		}
	}

	public void addTraining() {
		JsonVO j = new JsonVO();
		try {
			TrainingVO u = trainingService.save(training);
			j.setSuccess(true);
			j.setMsg("增加成功！");
			j.setObj(u);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}

	public void removeTraining() {
		trainingService.remove(training.getIds());
		JsonVO j = new JsonVO();
		j.setSuccess(true);
		j.setMsg("删除成功！");
		super.writeJson(j);
	}

	public void editTraining() {
		TrainingVO u = trainingService.edit(training);
		JsonVO j = new JsonVO();
		j.setSuccess(true);
		j.setMsg("编辑成功！");
		j.setObj(u);
		super.writeJson(j);
	}

}
